package cn.itsource.exam.controller;

import cn.itsource.exam.domain.Login;
import cn.itsource.exam.domain.StudentInfo;
import cn.itsource.exam.dtos.indto.UpdatePwdInDTO;
import cn.itsource.exam.query.UserInfoQuery;
import cn.itsource.exam.service.StudentInfoService;
import cn.itsource.exam.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by su on 2020/7/8.
 */
@RestController
@RequestMapping("/stu")
public class StudentInfoController {

    @Autowired
    private StudentInfoService studentInfoService;

    /**
     * 增加或修改用戶
     * @param user
     */
    @RequestMapping(value = "modify",method = RequestMethod.POST)
    public  String modifyUser(@RequestBody StudentInfo user, HttpServletRequest rq){
        Login info = (Login)rq.getSession().getAttribute(rq.getHeader("U-TOKEN"));
        return studentInfoService.modifyUser(user,info.getName());
    }

    /**
     * 刪除用戶
     * @param id
     */
    @RequestMapping(value = "del",method = RequestMethod.POST)
    public String delUser(Long id, HttpServletRequest rq){
        Login info = (Login)rq.getSession().getAttribute(rq.getHeader("U-TOKEN"));
        return studentInfoService.delUser(id, info.getName());
    }

    @RequestMapping(value = "batchdel",method = RequestMethod.POST)
    public String batchDelUser(String ids, HttpServletRequest rq){
        StudentInfo info = (StudentInfo)rq.getSession().getAttribute(rq.getHeader("U-TOKEN"));
        return studentInfoService.batchDelUser(ids,info.getName());
    }

    /**
     * 根据主键获取用户
     * @param id
     * @return
     */
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public StudentInfo getUserById(Long id){
        return studentInfoService.getUserById(id);
    }

    /**
     * 分页查询用户
     * @param paramIn
     * @return
     */
    @RequestMapping(value = "listpage",method = RequestMethod.POST)
    public PageList<StudentInfo> getUserList(@RequestBody UserInfoQuery paramIn){
        return studentInfoService.getUserList(paramIn);
    }

    @ResponseBody//返回json数据
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String uploadUser(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        if (file.isEmpty()) {
            return "文件为空！";
        }
        return studentInfoService.uploadUser(file,request);
    }
}
